﻿package com.Galaxisfarm {
	// Bibliotecas da game engine Flixel
	import com.adamatomic.flixel.*;

	public class DialogBox {
		// Sprite do ator
		[Embed(source = "../../data/dialog_box_actor.png")] private var ImgDialogBoxActor:Class;
		// Sprite do ator para indicar erro
		[Embed(source = "../../data/dialog_box_actor_error.png")] private var ImgDialogBoxActorError:Class;
		// Sprite Dialog Box
		[Embed(source = "../../data/dialog_box.png")] private var ImgDialogBox:Class;
		// Sprite Dialog Box Layer
		[Embed(source = "../../data/dialog_box_layer.png")] private var ImgDialogBoxLayer:Class;
		// Sprites da circunferência do personagem
		[Embed(source = "../../data/dialog_box_bg_bottom.png")] private var ImgDialogBoxBgBottom:Class;
		[Embed(source = "../../data/dialog_box_bg_up.png")] private var ImgDialogBoxBgUp:Class;
		[Embed(source = "../../data/dialog_box_bg_inner.png")] private var ImgDialogBoxBgInner:Class;
		// Fonte
		[Embed(source='../../data/verdana.ttf', fontName="fontVerdana", mimeType="application/x-font-truetype")] private var fontVerdana:Class;
		
		// Componentes da Dialog Box
		private var _components:FlxArray;
		
		// Construtor
		public function DialogBox(Message:String, Actor:Boolean = true):void {
			_components = new FlxArray;
			
			// Layer
			var layer:FlxSprite = new FlxSprite(ImgDialogBoxLayer, 20, 20, false, false, 640, 400);
			_components.add(layer);
			GameState.getLyrSprites().add(layer);
			
			if (Actor) {
				var bgInner:FlxSprite = new FlxSprite(ImgDialogBoxBgInner, 21, 324, false, false, 95, 95);
				_components.add(bgInner);
				GameState.getLyrSprites().add(bgInner);
				
				var bgUp:FlxSprite = new FlxSprite(ImgDialogBoxBgUp, 20, 322, false, false, 100, 49);
				_components.add(bgUp);
				GameState.getLyrSprites().add(bgUp);
				
				// Ator
				var actor:FlxSprite = new FlxSprite(ImgDialogBoxActor, 37, 298, true, false, 76, 120);
				actor.addAnimation("speak", [0, 1, 2, 3, 4, 5, 6, 7, 8], 5, true);
				actor.play("speak");
				_components.add(actor);
				GameState.getLyrSprites().add(actor);
				
				var bgBottom:FlxSprite = new FlxSprite(ImgDialogBoxBgBottom, 20, 371, false, false, 100, 49);
				_components.add(bgBottom);
				GameState.getLyrSprites().add(bgBottom);
			} else {
				// Ator Error
				var actorError:FlxSprite = new FlxSprite(ImgDialogBoxActorError, 20, 292, true, false, 100, 126);
				actorError.addAnimation("speak", [3, 4, 5, 6, 7, 8, 0, 1, 2], 5, true);
				actorError.play("speak");
				_components.add(actorError);
				GameState.getLyrSprites().add(actorError);				
			}
			
			// Dialog Box
			var dialogBox:FlxSprite = new FlxSprite(ImgDialogBox, 120, 295, false, false, 351, 90);
			_components.add(dialogBox);
			GameState.getLyrSprites().add(dialogBox);
			
			// Mensagem
			var message:FlxText = new FlxText(155, 300, 351, 90, Message, 0x000000, "fontVerdana", 12, "left");
			_components.add(message);
			GameState.getLyrSprites().add(message);
		}
		
		// Remove os componentes
		public function destroy():void {
			for (var i:int = (_components.length - 1); i >= 0; i--) {
				_components[i].kill();
				_components.remove(_components[i], true);
			}
		}
		
		// Obtém a lista de componentes
		public function getComponents():FlxArray {
			return _components;
		}
		
		// Adiciona um componente
		public function addComponent(Component:*):void {
			_components.add(Component);
			GameState.getLyrSprites().add(Component);
		}
		
		// Cria o botão de ok apenas para desaparecer
		// com os elementos da dialog box da tela
		public function createDefaultButtonOK():void {
			addComponent(new FlxButton(410, 365, new FlxSprite(null, 0, 0, false, false, 50, 20, 0xff25155E), function():void {
				destroy();
			}, new FlxSprite(null, 0, 0, false, false, 50, 20, 0xffE46713), new FlxText(0, 1, 50, 20, "OK", 0xffffff, null, 12, "center"), new FlxText(0, 1, 50, 20, "OK", 0xffffff, null, 12, "center")));
		}
	}
}